package com.usian.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.usian.common.enums.AppHttpCodeEnum;
import com.usian.common.exception.LeadException;
import com.usian.wemedia.entity.WmNewsMaterial;
import com.usian.wemedia.mapper.WmNewsMaterialMapper;
import com.usian.wemedia.service.IWmNewsMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 自媒体图文引用素材信息表 服务实现类
 * </p>
 *
 * @author usian
 * @since 2022-10-04
 */
@Service
public class WmNewsMaterialServiceImpl extends ServiceImpl<WmNewsMaterialMapper, WmNewsMaterial> implements IWmNewsMaterialService {

    @Override
    public void deleteByNewsId(Integer newId) {
        //根据自媒体文章id删除数据
        QueryWrapper<WmNewsMaterial> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(WmNewsMaterial::getNewsId,newId);
        boolean b = remove(queryWrapper);
        if(!b){
            throw new LeadException(AppHttpCodeEnum.SERVER_ERROR,"删除素材和文章关联关系失败");
        }
    }

    @Override
    public void saveInfoBatch(Integer wmNewsId, List<Integer> materialIds, int type) {
        List<WmNewsMaterial> list = new ArrayList<>();
        for (Integer materialId : materialIds) {
            WmNewsMaterial wmNewsMaterial = new WmNewsMaterial();
            wmNewsMaterial.setNewsId(wmNewsId);
            wmNewsMaterial.setMaterialId(materialId);
            wmNewsMaterial.setType(type);
            list.add(wmNewsMaterial);
        }
        boolean b = saveBatch(list);
        if(!b){
            throw new LeadException(AppHttpCodeEnum.SERVER_ERROR,"保存素材和文章关联关系失败");
        }
    }
}
